package com.rongshu.view.home.adapter.pageradapter;

import android.Manifest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.luck.picture.lib.photoview.PhotoView;
import com.rongshu.R;
import com.rongshu.utils.FileUtils;
import com.rongshu.utils.GlideUtils;
import com.utils.FastClickUtils;
import com.utils.permissions.OnPermission;
import com.utils.permissions.XXPermissions;

import java.util.List;

public class PreviewImgPageAdapter extends PagerAdapter {

    private List<String> imgPaths = null;
    //permission
    private String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE};

    public PreviewImgPageAdapter(List<String> list) {
        this.imgPaths = list;
    }

    @Override
    public int getCount() {
        return imgPaths == null ? 0 : imgPaths.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.adapter_preview_img_view, container, false);
        if (imgPaths.size() > 1){
            TextView txt_guide = view.findViewById(R.id.txt_guide);
            txt_guide.setText((position + 1) + "/" + imgPaths.size());
        }
        FrameLayout layout_save = view.findViewById(R.id.layout_save);
        layout_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleTarget<Bitmap> simpleTarget = new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull final Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
                        if (!XXPermissions.isHasPermission(container.getContext(), mPermissionList)) {
                            XXPermissions.with((Activity) container.getContext()).permission(mPermissionList).request(new OnPermission() {
                                @Override
                                public void hasPermission(List<String> granted, boolean isAll) {
                                    FileUtils.saveImageToSD(container.getContext(), bitmap);
                                }

                                @Override
                                public void noPermission(List<String> denied, boolean quick) {

                                }
                            });
                        } else {
                            FileUtils.saveImageToSD(container.getContext(), bitmap);
                        }
                    }
                };
                Glide.with(container.getContext())
                        .asBitmap()
                        .load(imgPaths.get(position))
                        .into(simpleTarget);
            }
        });
        PhotoView photoView = view.findViewById(R.id.iv_img);
        photoView.setZoomable(true);
        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!FastClickUtils.isFastClick()){
                    ((Activity) container.getContext()).finish();
                    ((Activity) container.getContext()).overridePendingTransition(R.anim.hold, R.anim.zoom_in_exit);
                }
            }
        });
        GlideUtils.loadImageView(container.getContext(), photoView, imgPaths.get(position));
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}
